package com.nodecollege.cloud.controller;

import com.nodecollege.cloud.common.annotation.ApiAnnotation;
import com.nodecollege.cloud.common.annotation.UserLoginAnnotation;
import com.nodecollege.cloud.common.constants.NCConstants;
import com.nodecollege.cloud.common.model.*;
import com.nodecollege.cloud.common.model.po.*;
import com.nodecollege.cloud.common.utils.ThreadUserUtils;
import com.nodecollege.cloud.service.RoleMemberService;
import com.nodecollege.cloud.service.RoleMenuService;
import com.nodecollege.cloud.service.RoleOrgService;
import com.nodecollege.cloud.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 角色管理
 *
 * @author LC
 * @date 2019/12/17 17:11
 */
@RestController
@UserLoginAnnotation(checkDataPower = true)
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private RoleMemberService roleMemberService;

    @Autowired
    private RoleMenuService roleMenuService;

    @Autowired
    private RoleOrgService roleOrgService;

    /**
     * 获取角色列表
     *
     * @param queryVO
     * @return
     */
    @ApiAnnotation(modularName = "角色管理", description = "获取角色列表")
    @PostMapping("/getRoleList")
    public NCResult<UpmsRole> getRoleList(@RequestBody QueryVO<UpmsRole> queryVO) {
        ApiDataPower apiDataPower = ThreadUserUtils.getLoginUser().getApiDataPower();
        apiDataPower.check(queryVO);
        apiDataPower.handle(queryVO);
        if (apiDataPower.getOrgList() == null) {
            queryVO.setOrgId(NCConstants.LONG_0);
        } else {
            queryVO.setOrgList(apiDataPower.getOrgList());
        }
        queryVO.getData().setRoleSource(NCConstants.INT_1);
        return roleService.getRoleList(queryVO);
    }

    /**
     * 添加角色
     *
     * @param upmsRole
     * @return
     */
    @ApiAnnotation(modularName = "角色管理", description = "添加角色")
    @PostMapping("/addRole")
    public NCResult addRole(@RequestBody UpmsRole upmsRole) {
        ThreadUserUtils.getLoginUser().getApiDataPower().check(upmsRole.getTenantId(), upmsRole.getOrgId(), null);
        upmsRole.setRoleSource(NCConstants.INT_1);
        roleService.addRole(upmsRole);
        return NCResult.ok("添加角色成功！");
    }

    /**
     * 更新角色
     *
     * @param upmsRole
     * @return
     */
    @ApiAnnotation(modularName = "角色管理", description = "更新角色")
    @PostMapping("/updateRole")
    public NCResult updateRole(@RequestBody UpmsRole upmsRole) {
        ThreadUserUtils.getLoginUser().getApiDataPower().check(upmsRole.getTenantId(), upmsRole.getOrgId(), null);
        upmsRole.setRoleSource(NCConstants.INT_1);
        roleService.updateRole(upmsRole);
        return NCResult.ok("更新角色成功！");
    }

    /**
     * 删除角色
     *
     * @param upmsRole
     * @return
     */
    @ApiAnnotation(modularName = "角色管理", description = "删除角色")
    @PostMapping("/delRole")
    public NCResult delRole(@RequestBody UpmsRole upmsRole) {
        ThreadUserUtils.getLoginUser().getApiDataPower().check(upmsRole.getTenantId(), upmsRole.getOrgId(), null);
        upmsRole.setRoleSource(NCConstants.INT_1);
        roleService.delRole(upmsRole);
        return NCResult.ok("删除角色成功！");
    }

    @ApiAnnotation(modularName = "角色管理", description = "锁定/解锁角色")
    @PostMapping("/lockRole")
    public NCResult lockRole(@RequestBody UpmsRole upmsRole) {
        ThreadUserUtils.getLoginUser().getApiDataPower().check(upmsRole.getTenantId(), upmsRole.getOrgId(), null);
        upmsRole.setRoleSource(NCConstants.INT_1);
        roleService.lockRole(upmsRole);
        return NCResult.ok();
    }

    @ApiAnnotation(modularName = "角色管理", description = "查询角色拥有成员列表")
    @PostMapping("/getMemberListByRoleId")
    public NCResult getMemberListByRoleId(@RequestBody QueryRelationVO<UpmsRole, UpmsMember> queryVO) {
        ApiDataPower apiDataPower = ThreadUserUtils.getLoginUser().getApiDataPower();
        apiDataPower.check(queryVO);
        apiDataPower.handle(queryVO);
        queryVO.setOrgId(null);
        queryVO.setMemberId(null);
        return roleMemberService.getMemberListByRoleId(queryVO);
    }

    @ApiAnnotation(modularName = "角色管理", description = "绑定角色成员关系")
    @PostMapping("/bindRoleMemberByRoleId")
    public NCResult bindRoleMemberByRoleId(@RequestBody BindVO bindVO) {
        ApiDataPower apiDataPower = ThreadUserUtils.getLoginUser().getApiDataPower();
        apiDataPower.check(bindVO);
        bindVO.setOrgId(null);
        bindVO.setMemberId(null);
        bindVO.setMainSource(true);
        return roleMemberService.bindRoleMember(bindVO);
    }

    @ApiAnnotation(modularName = "角色管理", description = "解绑角色成员关系")
    @PostMapping("/unbindRoleMember")
    public NCResult unbindRoleMember(@RequestBody UpmsRoleMember roleMember) {
        roleMember.setTenantId(ThreadUserUtils.getLoginUser().getTenantId());
        roleMemberService.unBindRoleMember(roleMember);
        return NCResult.ok();
    }

    @ApiAnnotation(modularName = "角色管理", description = "查询角色拥有菜单列表")
    @PostMapping("/getMenuListByRoleId")
    public NCResult<UpmsMenu> getMenuListByRoleId(@RequestBody UpmsRole role) {
        role.setTenantId(ThreadUserUtils.getLoginUser().getTenantId());
        role.setRoleSource(NCConstants.INT_1);
        return NCResult.ok(roleMenuService.getMenuListByRoleId(role));
    }

    @ApiAnnotation(modularName = "角色管理", description = "绑定/解绑角色菜单关系")
    @PostMapping("/bindRoleMenuByRoleId")
    public NCResult bindRoleMenuByRoleId(@RequestBody BindVO bindVO) {
        bindVO.setTenantId(ThreadUserUtils.getLoginUser().getTenantId());
        bindVO.setAppId(null);
        bindVO.setMainSource(true);
        roleMenuService.bindRoleMenu(bindVO);
        return NCResult.ok();
    }

    @ApiAnnotation(modularName = "角色管理", description = "查询角色拥有组织机构列表")
    @PostMapping("/getOrgListByRoleId")
    public NCResult getOrgListByRoleId(@RequestBody QueryRelationVO<UpmsRole, UpmsOrg> queryVO) {
        queryVO.getStartNode().setTenantId(ThreadUserUtils.getLoginUser().getTenantId());
        return roleOrgService.getOrgListByRoleId(queryVO);
    }

    @ApiAnnotation(modularName = "角色管理", description = "绑定/解绑角色组织机构关系")
    @PostMapping("/bindRoleOrgByRoleId")
    public NCResult bindRoleOrgByRoleId(@RequestBody BindVO bindVO) {
        bindVO.setTenantId(ThreadUserUtils.getLoginUser().getTenantId());
        bindVO.setMainSource(true);
        roleOrgService.bindRoleOrg(bindVO);
        return NCResult.ok();
    }
}
